/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WearhouseFrame.java
 *
 * Created on Jun 21, 2011, 11:25:55 AM
 */

package gbbmgmt;

import org.jdesktop.application.Action;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class WearhouseFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Wearhouse;

    /** Creates new form WearhouseFrame */
    public WearhouseFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBoxWearhouse = new javax.swing.JComboBox();
        jButtonFirst = new javax.swing.JButton();
        jButtonPrev = new javax.swing.JButton();
        jButtonNext = new javax.swing.JButton();
        jButtonLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldWearhouseID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldDesc = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldLoc = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonUpdate = new javax.swing.JButton();
        jButtonNew = new javax.swing.JButton();
        jButtonSubmit = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jComboBoxWearhouse.setName("jComboBoxWearhouse"); // NOI18N
        jComboBoxWearhouse.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxWearhousePopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(WearhouseFrame.class, this);
        jButtonFirst.setAction(actionMap.get("FirstRecord")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(WearhouseFrame.class);
        jButtonFirst.setText(resourceMap.getString("jButtonFirst.text")); // NOI18N
        jButtonFirst.setName("jButtonFirst"); // NOI18N

        jButtonPrev.setAction(actionMap.get("PrevRecord")); // NOI18N
        jButtonPrev.setText(resourceMap.getString("jButtonPrev.text")); // NOI18N
        jButtonPrev.setName("jButtonPrev"); // NOI18N

        jButtonNext.setAction(actionMap.get("NextRecord")); // NOI18N
        jButtonNext.setText(resourceMap.getString("jButtonNext.text")); // NOI18N
        jButtonNext.setName("jButtonNext"); // NOI18N

        jButtonLast.setAction(actionMap.get("LastRecord")); // NOI18N
        jButtonLast.setText(resourceMap.getString("jButtonLast.text")); // NOI18N
        jButtonLast.setName("jButtonLast"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextFieldWearhouseID.setText(resourceMap.getString("jTextFieldWearhouseID.text")); // NOI18N
        jTextFieldWearhouseID.setName("jTextFieldWearhouseID"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldDesc.setText(resourceMap.getString("jTextFieldDesc.text")); // NOI18N
        jTextFieldDesc.setName("jTextFieldDesc"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextFieldLoc.setName("jTextFieldLoc"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jButtonUpdate.setAction(actionMap.get("UpdateWearhouse")); // NOI18N
        jButtonUpdate.setText(resourceMap.getString("jButtonUpdate.text")); // NOI18N
        jButtonUpdate.setName("jButtonUpdate"); // NOI18N

        jButtonNew.setAction(actionMap.get("NewWearhouse")); // NOI18N
        jButtonNew.setText(resourceMap.getString("jButtonNew.text")); // NOI18N
        jButtonNew.setName("jButtonNew"); // NOI18N

        jButtonSubmit.setAction(actionMap.get("SubmitWearhouse")); // NOI18N
        jButtonSubmit.setText(resourceMap.getString("jButtonSubmit.text")); // NOI18N
        jButtonSubmit.setName("jButtonSubmit"); // NOI18N

        jButtonExit.setAction(actionMap.get("ExitWearhouse")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBoxWearhouse, 0, 276, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonPrev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLast)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldWearhouseID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldDesc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                    .addComponent(jTextFieldLoc, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSubmit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButtonLast)
                    .addComponent(jButtonNext)
                    .addComponent(jButtonPrev)
                    .addComponent(jButtonFirst)
                    .addComponent(jComboBoxWearhouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldWearhouseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldLoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonUpdate)
                    .addComponent(jButtonNew)
                    .addComponent(jButtonSubmit)
                    .addComponent(jButtonExit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxWearhousePopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxWearhousePopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
}//GEN-LAST:event_jComboBoxWearhousePopupMenuWillBecomeInvisible

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WearhouseFrame().setVisible(true);
            }
        });
    }

    public void LoadList(){
        jComboBoxWearhouse.removeAllItems();

        DBA.openDBCon();
        Wearhouse = DBA.RunSQLQuery("SELECT WearhouseID,WHDesc FROM Wearhouse");
        DBA.closeDBCon();

        for(String s[] : Wearhouse){
            jComboBoxWearhouse.addItem(s[0] + " - " + s[1]);
        }

        //Hide submit button.
        jButtonSubmit.setVisible(false);

        LastRecord();
    }

    @Action
    public void LastRecord() {
        jComboBoxWearhouse.setSelectedIndex(jComboBoxWearhouse.getItemCount()-1);

        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    public void SetData(){
        jTextFieldWearhouseID.setEditable(false);
        jTextFieldWearhouseID.setText(Integer.toString(DBA.WearhouseID));
        jTextFieldDesc.setText(DBA.WHDesc);
        jTextFieldLoc.setText(DBA.WHLoc);
    }

    @Action
    public void FirstRecord() {
        jComboBoxWearhouse.setSelectedIndex(0);

        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void PrevRecord() {
        if(jComboBoxWearhouse.getSelectedIndex()>=1){
            jComboBoxWearhouse.setSelectedIndex(jComboBoxWearhouse.getSelectedIndex()-1);
        }

        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void NextRecord() {
        if(jComboBoxWearhouse.getSelectedIndex() <= (jComboBoxWearhouse.getItemCount()-2)){
            jComboBoxWearhouse.setSelectedIndex(jComboBoxWearhouse.getSelectedIndex()+1);
        }

        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void ExitWearhouse() {
        dispose();
    }

    @Action
    public void NewWearhouse() {
        //Show Submit button.
        jButtonSubmit.setVisible(true);

        //Hide all appropriate buttons.
        jButtonUpdate.setVisible(false);
        jButtonNew.setVisible(false);
        jButtonFirst.setVisible(false);
        jButtonLast.setVisible(false);
        jButtonNext.setVisible(false);
        jButtonPrev.setVisible(false);
        jComboBoxWearhouse.setVisible(false);
        jTextFieldWearhouseID.setEditable(true);

        //Set text in all fields to blank.
        jTextFieldWearhouseID.setText("");
        jTextFieldDesc.setText("");
        jTextFieldLoc.setText("");
    }

    @Action
    public void SubmitWearhouse() {
        int response = JOptionPane.showConfirmDialog(null,
                "Warning, this record will be added to the database, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        if (response == 0){

            //Check to see if wearhouse ID is valid.  Check for an entry and if it exists.
            boolean kosher = true;
            if(jTextFieldWearhouseID.getText().equals("")) kosher = false;
            for(String s[] : Wearhouse){
                if(s[0].equals(jTextFieldWearhouseID.getText())){
                    kosher = false;
                }
            }
            if(Integer.parseInt(jTextFieldWearhouseID.getText()) <= 130) kosher = false;

            if(kosher){

                String fields = "WearhouseID,WHDesc,WHLoc";
                String query = "'" +
                    jTextFieldWearhouseID.getText() + "','" +
                    jTextFieldDesc.getText() + "','" +
                    jTextFieldLoc.getText() + "'";
                DBA.openDBCon();
                DBA.RunSQLInsertQuery("Wearhouse",fields,query);
                DBA.closeDBCon();

                //Show Submit button.
                jButtonSubmit.setVisible(false);

                //Hide all appropriate buttons.
                jButtonUpdate.setVisible(true);
                jButtonNew.setVisible(true);
                jButtonFirst.setVisible(true);
                jButtonLast.setVisible(true);
                jButtonNext.setVisible(true);
                jButtonPrev.setVisible(true);
                jComboBoxWearhouse.setVisible(true);
                jTextFieldWearhouseID.setEditable(false);

                LoadList();

            } else {
                JOptionPane.showMessageDialog(null, "Invalid or Duplicate Wearhouse ID Entered!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
            
        }
    }

    @Action
    public void UpdateWearhouse() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will modify wearhouse data, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        if (response == 0){

            DBA.openDBCon();

            String WHID = jTextFieldWearhouseID.getText();

            String query = "UPDATE Wearhouse SET WHDesc='" +
                        jTextFieldDesc.getText() + "',WHLoc='" +
                        jTextFieldLoc.getText() + "' WHERE WearhouseID = '" + WHID + "'";
            DBA.RunSQLUpdateQuery(query);

            DBA.closeDBCon();

        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonFirst;
    private javax.swing.JButton jButtonLast;
    private javax.swing.JButton jButtonNew;
    private javax.swing.JButton jButtonNext;
    private javax.swing.JButton jButtonPrev;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JButton jButtonUpdate;
    private javax.swing.JComboBox jComboBoxWearhouse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldDesc;
    private javax.swing.JTextField jTextFieldLoc;
    private javax.swing.JTextField jTextFieldWearhouseID;
    // End of variables declaration//GEN-END:variables

}
